package com.hrbxlh.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.DepartMentActionImpl;
import com.hrbxlh.actionImpl.ProfessDirectionActionImpl;
import com.hrbxlh.actionImpl.ProfessionActionImpl;
import com.hrbxlh.actionImpl.SiteActionImpl;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：ProfessionAction.java
* Description：专业action类
* History：
* Date           Author               Desc
* 2017.04.11	  xzx                  建立	
* -------------------------------------------------------
*/

@Controller
public class ProfessionAction {
	
	@Autowired
	@Qualifier("professionActionImpl")
	private ProfessionActionImpl professionActionImpl;
	
	@Autowired
	@Qualifier("departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;
	
	@Autowired
	@Qualifier("professDirectionActionImpl")
	private ProfessDirectionActionImpl professDirectionActionImpl;
	
	@Autowired
	@Qualifier("siteActionImpl")
	private SiteActionImpl siteActionImpl;
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	/**
	 * @description 删除专业(批量)
	 * @param request
	 * @param response
	 	//List<String> listUse = new ArrayList<String>();
		//listUse.add("10");
		//listUse.add("11");
		//jsonStr = mapper.writeValueAsString(listUse);
	 * @return   List<String>形式   jsonStr = ["1","2","3"...]
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delProfessions", method = RequestMethod.POST)
	public void delProfessions(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {			
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
            //删除
			Map<String, Object> result = professionActionImpl.delProfessions(paramList);
			boolean resultMarkUse = (Boolean)result.get("result");
			//返回参数
			resultMark = (resultMarkUse) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(!resultMarkUse){
				resultMap.put("failMsg",result.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除专业出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @description 跳转至添加专业页面 带全部地点信息
	 * @param request
	 * @param response
	 * 	//Map<String, Object> map = new HashMap<String, Object>();
		//map.put("departmentId", 1);
		//jsonStr = mapper.writeValueAsString(map);
	 * @return  跳转到添加专业页面
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipAddProfession", method = RequestMethod.GET)
	public ModelAndView skipAddProfession(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取全部地点信息
			List<Map<String, Object>> siteList = siteActionImpl.getSites();
			resultMap.put("siteList", siteList);
			//返回参数
			resultMap.put("departmentId", paramMap.get("departmentId"));
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//添加专业页面名称
			pageName = "pc/addProfession";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转添加专业页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @description 添加专业		
		插入专业表  专业看是不是重名了
		得到专业主键id
		插入专业方向表带着专业id  需要判断专业方向名字是否存在
		插入教室和专业的关联表
	 * @param request
	 * @param response
	//Map<String, Object> map = new HashMap<String, Object>();
	//map.put("professionName", "新加的vvv");
	//map.put("departmentId", 1);
	//List<String> list = new ArrayList<String>();
	//list.add("专业方向iii");
	//list.add("专业方向fff");
	//map.put("directions", list);
	//List<Integer> list1 = new ArrayList<Integer>();
	//list1.add(888);
	//list1.add(999);
	//map.put("sites", list1);
	//jsonStr = mapper.writeValueAsString(map);
	 * @return   
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addProfession", method = RequestMethod.POST)
	public void addProfession(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			Map<String, Object> professionAddResult = professionActionImpl.addProfession(paramMap);
			boolean addProfession = (Boolean)professionAddResult.get("result");
			//添加专业成功
			if(addProfession){
				paramMap.put("professionId", professionAddResult.get("professionId"));
				Map<String, Object> addDirectResult = professDirectionActionImpl.addDirect(paramMap);
				//如果成功
				if((Boolean)addDirectResult.get("result")){
					//添加教学地点与专业的关系
					boolean addSiteReProfession = siteActionImpl.addSiteReProfession(paramMap);
					if(addSiteReProfession){
						resultMap.put("result", "success");
					}else{
						resultMap.put("result", "fail");
						resultMap.put("failMsg", "添加失败");
					}
				}else{
					resultMap.put("result", "fail");
					resultMap.put("failMsg", addDirectResult.get("failMsg"));
				}
			}else{
				resultMap.put("result", "fail");
				resultMap.put("failMsg", professionAddResult.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加专业出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @description 跳转至修改专业页面 带全部地点信息
	 * @param request  专业id
	 * @param response
	 * //Map<String, Object> map = new HashMap<String, Object>();
		//map.put("professionId", 21);
		//jsonStr = mapper.writeValueAsString(map);
	 * @return  跳转到修改专业页面
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/skipSaveProfession", method = RequestMethod.GET)
	public ModelAndView skipSaveProfession(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//获取全部地点信息
			List<Map<String, Object>> siteList = siteActionImpl.getSites();
			resultMap.put("allSiteList", siteList);
			//获取专业信息
			Map<String,Object> professionMap = professionActionImpl.getProfessionDetail(paramMap);
			resultMap.put("profession", professionMap);
			//获取专业的专业方向信息
			List<Map<String, Object>> directList = professDirectionActionImpl.getDirectByprofessId(paramMap);
			resultMap.put("directList", directList);
			//获取专业的关联地点信息
			List<Map<String, Object>> mySiteList = siteActionImpl.getSiteReProfession(paramMap);
			resultMap.put("mySiteList", mySiteList);
			//获取我的消息
			List<Map<String, Object>> noticeList  = courseActionImpl.getMyNotice(request);
			resultMap.put("noticeList", noticeList);
			//修改专业页面名称
			pageName = "pc/saveProfession";
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转修改专业页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @description 修改专业		
		修改专业表  专业看是不是重名了
		得到专业主键id
		插入专业方向表带着专业id  需要判断专业方向名字是否存在
		插入教室和专业的关联表
	 * @param request
	 * @param response
	 * //Map<String, Object> map = new HashMap<String, Object>();
		//map.put("professionId", 21);
		//map.put("professionName", "新加的English2341");
		//List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		//Map<String, Object> map1 = new HashMap<String, Object>();
		//map1.put("mainId", 16);
		//map1.put("directName", "专业方向iii");
		//list.add(map1);
		//Map<String, Object> map2 = new HashMap<String, Object>();
		//map2.put("mainId", 17);
		//map2.put("directName", "专业方向fff");
		//list.add(map2);
		//Map<String, Object> map3 = new HashMap<String, Object>();
		//map3.put("mainId", 0);
		//map3.put("directName", "专业方向eeengaaaa");
		//list.add(map3);
		//map.put("directions", list);
		//List<Integer> list1 = new ArrayList<Integer>();
		//list1.add(888);
		//list1.add(999);
		//list1.add(1010);
		//map.put("sites", list1);
		//jsonStr = mapper.writeValueAsString(map);
	 * @return   
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveProfession", method = RequestMethod.POST)
	public void saveProfession(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			Map<String, Object> professionSaveResult = professionActionImpl.saveProfession(paramMap);
			boolean saveProfession = (Boolean)professionSaveResult.get("result");
			//修改专业成功
			if(saveProfession){
				Map<String, Object> saveDirectResult = professDirectionActionImpl.saveDirectList(paramMap);
				//如果成功
				if((Boolean)saveDirectResult.get("result")){
					//添加教学地点与专业的关系
					boolean addSiteReProfession = siteActionImpl.addSiteReProfession(paramMap);
					if(addSiteReProfession){
						resultMap.put("result", "success");
					}else{
						resultMap.put("result", "fail");
						resultMap.put("failMsg", "修改失败");
					}
				}else{
					resultMap.put("result", "fail");
					resultMap.put("failMsg", saveDirectResult.get("failMsg"));
				}
			}else{
				resultMap.put("result", "fail");
				resultMap.put("failMsg", professionSaveResult.get("failMsg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("修改专业出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @description 删除专业方向
	 * @param request
	 * @param response
	 * //Map<String, Object> map = new HashMap<String, Object>();
		//map.put("mainId", 18);
		//jsonStr = mapper.writeValueAsString(map);
	 * @return   
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delProfessDirection", method = RequestMethod.POST)
	public void delProfessDirection(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ProfessionAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
            //删除
			boolean resultMarkUse = professDirectionActionImpl.delDirect(paramMap);
			//返回参数
			resultMark = (resultMarkUse) ? "success" : "fail";
			resultMap.put("result", resultMark);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除专业方向出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
}